package com.example.demo3.config;

import com.example.demo3.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class myWebConfig implements WebMvcConfigurer {


    //获取图片请求url
    @Value("${upload.handler}")
    private String handlerImg;

    //获取图片保存位置
    @Value("${upload.filePath}")
    private String filePath;


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        // 设置允许跨域的路径
        registry.addMapping("/**")
                // 设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                // 设置允许的请求方式
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                // 设置允许的header属性
                .allowedHeaders("*")
                // 是否允许cookie
                .allowCredentials(true)
                // 设置允许跨域的时长
                .maxAge(3600);
    }

    @Bean
    public MyInterceptor createInterceptorBean(){
        return new MyInterceptor();
    }

    /**
     * 拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        addInterceptor(new MyInterceptor())配置拦截器
//        addPathPatterns("/**")不拦截url
//        excludePathPatterns不拦截url swagger
        registry.addInterceptor(createInterceptorBean()).addPathPatterns("/**")
                .excludePathPatterns(handlerImg)
                .excludePathPatterns("/swagger-ui.html")
                .excludePathPatterns("/file/**")
                .excludePathPatterns("/user/register")
                .excludePathPatterns("/user/selectAccount")
                .excludePathPatterns("/index.html")
                .excludePathPatterns("/images/**")
                .excludePathPatterns("/user/select")
                .excludePathPatterns("/user/selectList")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/user/exit")
                .excludePathPatterns("/user/delete")
                .excludePathPatterns("/user/update")
                .excludePathPatterns("/user/add")
                .excludePathPatterns("/file/upload")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
    }


    /**
     * swagger 静态资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler(handlerImg)
                .addResourceLocations("file:"+filePath);
    }


}
